package com.borderbears.frontend.camera
{
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;
	
	import com.baseoneonline.flash.utils.logger.Logger;
	import com.borderbears.frontend.util.Const;
	
	import flash.utils.getQualifiedClassName;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.DAE;
	
	public class CameraSystem extends DisplayObject3D
	{
		private var log:Logger = Logger.getInstance(getQualifiedClassName(this));
		
		private var tempCam:Camera3D;
		private var camera:Camera3D
		
		private var camNodes:Array = [];
		
		// In seconds
		private var tweenTime:Number = Const.CAMERA_TWEEN_TIME;

		private var currentCam:int = -1;
		
		private var tweakX:Number = 0;
		private var tweakY:Number = 0;
		
		public function CameraSystem()
		{
			tempCam = new Camera3D();
			camera = new Camera3D(54, 1, 20);
			camera.zoom = 105;
		}
		
		public function getCamera():Camera3D {
			return camera;
		}
		
		/**
		 * Tries to find couples of nodes (camera + target)
		 * and stores them in this system.
		 * 
		 * @param dae	The collada model to look in
		 * @param max	Maximum number of cameras to be found
		 * 
		 */
		public function findCamNodes(dae:DAE, max:int = 10):void {
			log.info("Looking for camNodes ('"+Const.OBJECT_CAMERA
				+"#' and '"+Const.OBJECT_CAMERA_TARGET+"#"
				+Const.OBJECT_CAMERA_TARGET+"')");
			camNodes = [];
			for(var i:int=0; i<max; i++) {
				var camObj:DisplayObject3D = dae.getChildByName(Const.OBJECT_CAMERA+i,true);
				if (null != camObj) {
					var camTar:DisplayObject3D = dae.getChildByName(Const.OBJECT_CAMERA+i+Const.OBJECT_CAMERA_TARGET, true);
					if (null != camTar) {
						log.info("Camera "+i+" found.");
						var camNode:CamNode = new CamNode(camObj, camTar);
						camNodes.push(camNode);
					}
				}
			}
			nextCamera();
		}
		
		public function setCamera(camNumber:int):void {
			currentCam = camNumber;
			log.info("Switching to camera "+currentCam);
			var nextCam:CamNode = camNodes[currentCam];
			
			Tweener.addTween(tempCam, {
				x:nextCam.camera.position.x,
				y:nextCam.camera.position.y,
				z:nextCam.camera.position.z,
				rotationX:nextCam.camera.rotationX,
				rotationY:nextCam.camera.rotationY,
				rotationZ:nextCam.camera.rotationZ,
				time:tweenTime,
				transition:Equations.easeOutCubic
			});
				
		}
		
		public function tweakAim(x:Number, y:Number):void {
			tweakX = x;
			tweakY = y;
		}
		
		public function nextCamera():void {
			
			if (camNodes.length > 1) {
				currentCam++;
				if (currentCam > camNodes.length-1) {
					currentCam = 0;
				}
				setCamera(currentCam);
			}
		}
		
		public function update(t:Number):void {
			var tweakMult:Number = 10;
			var smooth:Number = .1;
			camera.position = tempCam.position;
			camera.rotationX += (((tempCam.rotationX + (tweakMult*tweakY))-camera.rotationX)*smooth);
			camera.rotationY += (((tempCam.rotationY + (tweakMult*-tweakX))-camera.rotationY)*smooth);
			camera.rotationZ = tempCam.rotationZ;

		}
		

	}
}